#include <Windows.h>
#include <string>
#include <sstream>
using namespace std;
const int TIMER_ID = 1;
const int WINDOW_WIDTH = 200;
const int WINDOW_HEIGHT = 100;
const int COUNTDOWN_INTERVAL = 1000 * 60 * 10; // 10分钟倒计时，单位为毫秒

HWND hwnd;
int countdown = 0;
bool isCountingDown = false;
void UpdateTimerText() {
    ostringstream oss;
    oss << "time: " << countdown / 1000 << " s";
//    string text = "倒计时: " + to_string(countdown / 1000) + " 秒";
    SetWindowTextA(hwnd, oss.str().c_str());
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CREATE:
            SetTimer(hwnd, TIMER_ID, 1000, NULL); // 每隔1秒触发计时器
            break;
        case WM_TIMER:
            if (wParam == TIMER_ID) {
                if (isCountingDown) {
                    countdown -= 1000;
                    if (countdown <= 0) {
                        countdown = COUNTDOWN_INTERVAL;
                    }
                    UpdateTimerText();
                }
            }
            break;
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            RECT clientRect;
            GetClientRect(hwnd, &clientRect);
            DrawTextA(hdc, "daojishi", -1, &clientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            EndPaint(hwnd, &ps);
            break;
        }
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    const char *className = "CountdownTimer";
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WindowProc,
                     0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW),
                     NULL, NULL, className, NULL};
    RegisterClassEx(&wc);
    hwnd = CreateWindow(className, "daojishi", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                        WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
    if (hwnd == NULL) {
        return 0;
    }
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    countdown = COUNTDOWN_INTERVAL;
    isCountingDown = true;
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}